package com.github.binarylei.hadoop.test;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: leigang
 * @version: 2018-05-24
 */
public class Main {

    private static AtomicInteger i = new AtomicInteger(1);

    public static void main(String[] args) throws Exception {
        String path ="F:\\资料\\java文档\\code-2018\\hadoop\\hadoop-api\\template";

        ExecutorService pool = Executors.newFixedThreadPool(10);
        while (true) {
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        createXdrFile(path, i.getAndIncrement());
                    } catch (Exception e) {
                        ;
                    }
                }
            });
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                ;
            }
        }
    }

    public static void createXdrFile(String path, long n) {
        long start = System.currentTimeMillis();
        FileOutputStream out = null;
        try {
            File file = new File(path);
            FileUtils.forceMkdir(file);
            String filename = file + File.separator + n + ".txt";
            out = new FileOutputStream(filename);
            System.out.println("正在生成文件：" + filename);
            for (int i = 0; i < 100000; i++) {
                IOUtils.write(XDR.createXdr() + IOUtils.LINE_SEPARATOR, out, "utf-8");
            }
            System.out.println("生成文件耗时：" + (System.currentTimeMillis() - start));
        } catch (IOException e) {
            ;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    ;
                }
            }
        }
    }

    public static class XDR {

        public static String createXdr() {
            String str = "";
            for (int i = 0; i < 26; i++) {
                str += new Random().nextInt() + "|";
            }
            str += "$";
            return str;
        }
    }
}
